// store.js
import { createStore } from 'vuex';
const store = createStore({
  state() {
    return {
      data:[],
      sessionMsg:[],
      apiHost:"https://api.openai.com",
      userInfo:{},
      sayType:1,
      sayId:"",
      status:true,
    };
  },
  mutations: {
    //是否正在会话
    setStatus(state,value){
        state.status=value
    },
    //本地存储所有数据
    setData(state,value){
        localStorage.setItem("data",JSON.stringify(value))
        state.data=value
    },
    //存储当前会话并同步数据
    setSessionMsg(state,value) {
      localStorage.setItem("sessionMsg",JSON.stringify(value))
      var data=state.data=state.data.map(item=>{
        if(item.id==state.sayId){
            item.sessionMsg=value
        }
        return item
      })
      state.data=data
      localStorage.setItem("data",JSON.stringify(data))
      state.sessionMsg=value
    },
    //切换会话窗口
    setTabSessionMsg(state,value) {
        localStorage.setItem("sessionMsg",JSON.stringify(value))
        state.sessionMsg=value
    },
    //存储当前选中会话
    setSayType(state,value) {
        state.sayType=value.type
        state.sayId=value.id
    },
    //存储登入用户信息
    setUserInfo(state,value) {
        state.userInfo=value
    }
  }
});

export default store
